// pages/kuaidi/kuaidi.js
const db = wx.cloud.database();
import {
  getTimeNow
} from '../../utils/index';
Page({

  data: {
    a: '',
    //typeList: ['小件','中件','大件'],
    typeList: [{
        name: '小件',
        tips: '小件：<牛奶盒大小>一般距离，底价：2元',
        money: 2
      },
      {
        name: '中件',
        tips: '中件：<鞋盒大小>一般距离，底价：3元',
        money: 3
      },
      {
        name: '大件',
        tips: '大件：<2公斤左右>一般距离，底价：6元',
        money: 6
      }
    ],

    list: [{
        name: '立即发布',
        color: 'green'
      },

    ],
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    array: ['其他地址请在备注中填写', '北校区C4快递超市', '北校区C4底层优洗坊', '汇江楼A13宿舍楼下菜鸟驿站'],
    array2: ['不限制', '尽快送达', '2小时内', '今天中午', '今天下午', '2天内'],
    array3: ['不限制', '男生', '女生'],
    array4: ['微信', 'QQ'],
    typeNow: 0, //当前tab
    showMore: false,
    shangjing: false,
    selectBusiness: false,
    money: 2,
    addMoney: 0,
    imgUrl: '',
    remark: '无',
    phonenumber: '',
    userInfo: '',
    WX: false,
    QQ: '',
    lookNumber: '',
    orderid: '',
    qujian: '',
  },
    // 跳转到免责声明
    gotoBook() {
      wx.navigateTo({
        url: '/pages/agreementS/agreementS',
      })
       
    },
  selectType(res) {
    const {
      id,
      tip
    } = res.currentTarget.dataset;
    this.setData({
      typeNow: id,
      money: this.data.typeList[id].money,
    })

    wx.showToast({
      icon: 'none',
      title: tip,
      duration: 2800, // 延迟的时间

    })


    /*     setTimeout(() => {  
          wx.reLaunch({
            url: '/pages/my/my'
          })
        }, 500) */
  },

  // 打赏
  getaddMoney(e) {
    // console.log(typeof(Number(e.detail.value)) )   Number() 将字符串转换成 数字
    this.setData({
      addMoney: Number(e.detail.value)
    })
  },

  //点击联系我,并复制出来  公告
  get() {
    wx.showModal({
      confirmColor: '#00FF7F', //确定的颜色
      /*  cancelColor:'#FF4040',  //取消的颜色 */
      showCancel: false,
      title: '点击复制微信号',
      content: '注意：身份码每隔10分钟刷新一次,建议10分钟内无接单，可在我的订单中删除后重新发布.加急单可直接私信我，1分钟内派接单.如有纠纷或获取补贴请联系我-微信：YXHLYF19115314436',
      success(res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: 'YXHLYF19115314436',
            success: function (res) {
              wx.showToast({
                title: '微信号已复制',
                //icon:"none",
                image: "/imgs/提交成功.png", //只能是本地的
                mask: "true" //是否设置点击蒙版，防止点击穿透
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },


  aa(res) {
    this.setData({
      index4: res.detail.value,
      WX: true

    })
  },

  bb(res) {
    this.setData({
      index: res.detail.value,
      selectBusiness: true
    })
  },
  cc(res) {
    this.setData({
      index2: res.detail.value,
    })
  },
  dd(res) {
    this.setData({
      index3: res.detail.value,

    })
  },
  change(res) {
    //console.log(res)
    const value = res.detail.value;
    this.setData({
      shangjing: value
    })
  },
  showMore() {
    this.setData({
      showMore: !this.data.showMore
    })
  },

  /*   selectType (res){
      this.setData({
        typeNow: res.currentTarget.dataset.id
      })
    }, */


  onLoad: function (options) {
    /*     var userInfo = wx.getStorageSync('key');
        console.log(userInfo);
        this.setData({userInfo:userInfo}) */



    // 过审核
    const that = this
    db.collection("a").get()
      .then(res => {
        console.log("11111111", res.data[0].a)
        that.setData({
          a: res.data[0].a,
        })
      })







    this.setData({
      modalName: 'Modal'
    })

    const userInfo = wx.getStorageSync('key')
    console.log(userInfo.avatarUrl)
    this.setData({
      userInfo: userInfo.avatarUrl,
    })
    // 自定义 tarb 标题
    wx.setNavigationBarTitle({
      title: '发布快递代取',
    })
  },

  /* 跳转到选择收件地址页面 */
  /*   selectAddress() {
      wx.navigateTo({
        url: '/pages/selectAddress/selectAddress',
      })
    }, */
  /* 取件号码 */
  getqujian(e) {
    this.setData({
      qujian: e.detail.value,
    })
  },

  /* 收件地址 */
  getaddress(e) {
    this.setData({
      address: e.detail.value,
      lookNumber: 0
    })
  },

  /* 联系电话 */
  getphonenumber(e) {
    this.setData({
      phonenumber: e.detail.value
    })
  },

  /* 备注信息 */
  getRemark(e) {
    this.setData({
      remark: e.detail.value
    })
  },

  /* 获取QQ/WX  */
  getQQ(e) {
    console.log("321", e)
    this.setData({
      QQ: e.detail.value
    })
  },




  submit() {
    const myinfo = wx.getStorageSync('key');
    if (myinfo === null || myinfo === '') {
      wx.showToast({
        title: '抱歉！您暂未登录！请先去登录',
        icon: "none"
      })
      return;
    }

    function setTimeDateFmt(s) { // 个位数补齐十位数
      return s < 10 ? '0' + s : s;
    }
    const now = new Date()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let hour = now.getHours()
    let minutes = now.getMinutes()
    let seconds = now.getSeconds()
    month = setTimeDateFmt(month)
    day = setTimeDateFmt(day)
    hour = setTimeDateFmt(hour)
    minutes = setTimeDateFmt(minutes)
    seconds = setTimeDateFmt(seconds)
    let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();
    this.setData({
      orderid: orderCode
    })
    // console.log(orderCode)
    // 日期 + 6位随机数的订单号。 (年月日 + 分钟 + 毫秒)
    //wx.setStorageSync('orderid', orderCode)
    // 保存this指向
    const that = this.data;
    // 判断必填值有没有填
    // 收件地址、快递单家、或者
    if (!that.address || !that.phonenumber || !that.array[that.index] || !that.qujian) {
      wx.showToast({
        icon: 'none',
        title: '您填写的信息不全',
      })
      return;
    }

    // 允许接收订阅 消息
    //const templateId = '6TvIGy7QzeVGqZJCySdrfjmMOpNvGktM0j_A2PEVSxQ' 
    
/*     wx.requestSubscribeMessage({
      tmplIds: [templateId], 
      success(res) {
        console.log('同意：', res[templateId] === 'accept')
        if (res[templateId] === 'accept') {
          wx.showToast({
            title: '订阅成功,注意查看推送消息',
            icon: "none",
            duration: 1000
          }) */
          wx.requestSubscribeMessage({
            tmplIds: ['y5vuXP2krfP6TloZ7iebiH2yoB8DJR3g3fjjcsGzcNY','6TvIGy7QzeVGqZJCySdrfjmMOpNvGktM0j_A2PEVSxQ'], 
            success(res) {
              console.log(res)
              //console.log('同意：', res[templateId] === 'accept')
                if (res['y5vuXP2krfP6TloZ7iebiH2yoB8DJR3g3fjjcsGzcNY'] === 'accept') {
                wx.showToast({
                  title: '订阅成功,注意查看推送消息',
                  icon: "none",
                  duration: 1000
                })
          db.collection('order').add({
            data: {
              //取件号码
              qujian: that.qujian,
              // 订单编号
              orderid: that.orderid,
              // 浏览量
              lookNumber: that.lookNumber,
              // 模块的名字
              name: '快递代取',
              // 当前时间
              time: getTimeNow(),
              // 订单金额
              money: that.money + that.addMoney,
              // 联系电话
              phonenumber: that.phonenumber,
              // QQ
              QQ: that.QQ,
              // QQ/微信
              selectQQ: that.array4[that.index4],
              // 订单状态
              state: '待帮助',
              // 收件地址
              address: that.address,
              // 取件地址
              Businessaddress: that.array[that.index],
              // 性别限制
              expectGender: that.array3[that.index3],
              // 备注信息
              remark: that.remark,
              // 期望送达
              expectTime: that.array2[that.index2],
              // 快递大小
              size: that.typeList[that.typeNow].name,
              // 订单信息
              info: {
                size: that.typeList[that.typeNow].name,
                remark: that.remark,
                expectTime: that.array2[that.index2],
                expectGender: that.array3[that.index3],
              },
              //用户信息
              userInfo: that.userInfo,
              createTime: db.serverDate()
            },
            success: (res) => {
              setTimeout(() => {
                wx.switchTab({
                  url: '../get/get',
                })
              }, 500)
            }
          })


        } else {
          wx.showToast({
            title: '订阅失败,发布订单失败',
            icon: "none",
            duration: 1000
          })
          /*             setTimeout(()=>{  
                        wx.switchTab({
                          url: '../index/index',
                        })
                      },1000) */

        }  


      }
    })




    /* db.collection('order').add({
        data: {
            //取件号码
            qujian: that.qujian,
            // 订单编号
            orderid: that.orderid,
            // 浏览量
            lookNumber: that.lookNumber,
            // 模块的名字
            name: '快递代取',
            // 当前时间
            time: getTimeNow(),
            // 订单金额
            money: that.money + that.addMoney,
            // 联系电话
            phonenumber:that.phonenumber,
            // QQ
            QQ:that.QQ,
            // QQ/微信
            selectQQ: that.array4[that.index4],
            // 订单状态
            state: '待帮助',
            // 收件地址
            address: that.address,
            // 取件地址
            Businessaddress: that.array[that.index],
            // 性别限制
            expectGender: that.array3[that.index3],
            // 备注信息
            remark: that.remark,
            // 期望送达
            expectTime: that.array2[that.index2],
            // 快递大小
            size: that.typeList[that.typeNow].name,
            // 订单信息
            info: {
                size: that.typeList[that.typeNow].name,
                remark: that.remark,
                expectTime: that.array2[that.index2],
                expectGender: that.array3[that.index3],
           },
            //用户信息
            userInfo: that.userInfo,
            createTime: db.serverDate()
        },
        success: (res) => {

        }
    }) */





    /*   setTimeout(()=>{  
        wx.switchTab({
          url: '../get/get',
        })
      },1000) */


  },



  // 返回 
  return () {
    wx.switchTab({
      url: '/pages/index/index',
    })

  },







  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },












})